package net.thxjj.adminServer.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import net.thxjj.adminServer.entity.StaticBean;
import net.thxjj.adminServer.entity.aep.DevLog;
import net.thxjj.adminServer.entity.aep.HeartData;
import net.thxjj.adminServer.entity.aep.SmokeData;
import net.thxjj.adminServer.mongodao.MongoEventDao;
import net.thxjj.adminServer.mongodao.MongoHeartDao;
import net.thxjj.adminServer.service.IDevHistoryDeepService;
import net.thxjj.adminServer.util.DataUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DevHistoryDeepService implements IDevHistoryDeepService {

    @Autowired
    private MongoHeartDao mongoHeartDao;
    @Autowired
    private MongoEventDao mongoEventDao;

    @Override
    public List<List<Object>> smokeStateHistoryList(String device_id, long start_time, long end_time) {

        List<SmokeData> list = mongoEventDao.getSmokeStateHistory(device_id,start_time,end_time);
        List<List<Object>> ls = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            SmokeData parameterMap = list.get(i);
            long smoke_state = parameterMap.getSmoke_state();
            long timestamp = parameterMap.getTimestamp();
            List<Object> list1 = new ArrayList<>();
            if(smoke_state==1){
                smoke_state = 0;
            }else {
                smoke_state = 3;
            }
            list1.add(timestamp);
            list1.add(smoke_state);
            ls.add(list1);

        }
        return ls;
    }

    @Override
    public List<List<Object>> faultHistoryList(String device_id, long start_time, long end_time) {
        List<DevLog> list = mongoEventDao.getFaultDataAtTime(device_id,start_time,end_time);
        List<List<Object>> lists = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            List<Object> l = new ArrayList<>();
            DevLog devLog = list.get(i);
            l.add(devLog.getCreateTime());
            long value = 0;
            try {
                JSONObject jsonObject = JSON.parseObject(devLog.getEventContent());
                long vb = jsonObject.getLongValue("error");
                if(vb==1){
                    value = 4;
                }
            }catch (JSONException e){
                e.printStackTrace();
            }

            l.add(value);
            lists.add(l);
        }
        return lists;
    }

    @Override
    public List<List<Object>> lowVoltHistoryList(String device_id, long start_time, long end_time) {
        List<DevLog> list = mongoEventDao.getLowVoltDataAtTime(device_id,start_time,end_time);
        List<List<Object>> lists = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            List<Object> l = new ArrayList<>();
            DevLog devLog = list.get(i);
            l.add(devLog.getCreateTime());
            long value = 0;
            try {
                JSONObject jsonObject = JSON.parseObject(devLog.getEventContent());
                long vb = jsonObject.getLongValue("error");
                if(vb==3){
                    value = 1;
                }
            }catch (JSONException e){
                e.printStackTrace();
            }

            l.add(value);
            lists.add(l);
        }
        return lists;
    }

    @Override
    public List<List<Object>> dismantleHistoryList(String device_id, long start_time, long end_time) {
        List<DevLog> list = mongoEventDao.getDismantleDataAtTime(device_id,start_time,end_time);
        List<List<Object>> lists = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            List<Object> l = new ArrayList<>();
            DevLog devLog = list.get(i);
            l.add(devLog.getCreateTime());
            long value = 0;
            try {
                JSONObject jsonObject = JSON.parseObject(devLog.getEventContent());
                long vb = jsonObject.getLongValue("tamper_alarm");
                if(vb==1){
                    value = 2;
                }
            }catch (JSONException e){
                e.printStackTrace();
            }

            l.add(value);
            lists.add(l);
        }
        return lists;
    }

    @Override
    public List<List<Object>> heartHistoryList(String device_id, long start_time, long end_time) {
        List<HeartData> list = mongoHeartDao.getHeartBeatList(device_id,start_time,end_time);
        List<List<Object>> liss = new ArrayList<>();
        for (int i=0;i<list.size();i++){
            HeartData heartData = list.get(i);
            List<Object> l = new ArrayList<>();
            l.add(heartData.getTimestamp());
            l.add(1);
            liss.add(l);
        }
        return liss;
    }

    @Override
    public StaticBean snrStatic(String device_id, long start_time, long end_time) {
        List<SmokeData> list = mongoEventDao.getSnrHistory(device_id,start_time,end_time);
        double max=0;
        double min=0;
        double sum=0;
        long count = 0;
        List<List<Object>> listcur = new ArrayList<>();
        for (SmokeData map:list) {
            long value = map.getSnr();
                count++;
                if(count==1){
                    max = value;
                    min = value;
                }

                sum+=value;
                if(max<value){
                    max = value;
                }
                if(min>value){
                    min = value;
                }
                long timestamp = map.getTimestamp();
                List<Object> lista = new ArrayList<>();
                lista.add(timestamp);
                lista.add(value);
                listcur.add(lista);
        }

        StaticBean staticBean = new StaticBean();
        if(count>0){
            staticBean.setMin(DataUtils.getDoubleString(min)).setMax(DataUtils.getDoubleString(max)).setAvg(String.format("%.2f", sum/count));
        }else{
            staticBean.setMin("").setMax("").setAvg("");
        }
        staticBean.setHistoryList(listcur);

        return staticBean;
    }

    @Override
    public StaticBean rsrpStatic(String device_id, long start_time, long end_time) {
        List<HeartData> list = mongoHeartDao.getHeartBeatList(device_id,start_time,end_time);
        List<List<Object>> liss = new ArrayList<>();
        for (int i=0;i<list.size();i++){
            HeartData heartData = list.get(i);
            List<Object> l = new ArrayList<>();
            l.add(heartData.getTimestamp());
            l.add(heartData.getRsrp());
            liss.add(l);
        }

        StaticBean staticBean =mongoHeartDao.getRsrpStatic(device_id,start_time,end_time);
        if(staticBean==null){
            staticBean=new StaticBean().setAvg("").setMax("").setMin("").setHistoryList(liss);
        }else{
            staticBean.setHistoryList(liss);
        }

        return staticBean;
    }

    @Override
    public StaticBean eclStatic(String device_id, long start_time, long end_time) {
        List<HeartData> list = mongoHeartDao.getHeartBeatList(device_id,start_time,end_time);
        List<List<Object>> liss = new ArrayList<>();
        for (int i=0;i<list.size();i++){
            HeartData heartData = list.get(i);
            List<Object> l = new ArrayList<>();
            l.add(heartData.getTimestamp());
            l.add(heartData.getEcl());
            liss.add(l);
        }
        StaticBean staticBean =mongoHeartDao.getEclStatic(device_id,start_time,end_time);
        if(staticBean==null){
            staticBean=new StaticBean().setAvg("").setMax("").setMin("").setHistoryList(liss);
        }else{
            staticBean.setHistoryList(liss);
        }

        return staticBean;
    }

    @Override
    public StaticBean signalPowerStatic(String device_id, long start_time, long end_time) {
        List<SmokeData> list = mongoEventDao.getSignalPowerHistory(device_id,start_time,end_time);
        double max=0;
        double min=0;
        double sum=0;
        long count = 0;
        List<List<Object>> listcur = new ArrayList<>();
        for (SmokeData map:list) {
            long value = map.getSignalPower();
                count++;
                if(count==1){
                    max = value;
                    min = value;
                }

                sum+=value;
                if(max<value){
                    max = value;
                }
                if(min>value){
                    min = value;
                }
                long timestamp = map.getTimestamp();
                List<Object> lista = new ArrayList<>();
                lista.add(timestamp);
                lista.add(value);
                listcur.add(lista);
        }


        StaticBean staticBean = new StaticBean();
        if(count>0){
            staticBean.setMin(DataUtils.getDoubleString(min)).setMax(DataUtils.getDoubleString(max)).setAvg(String.format("%.2f", sum/count));
        }else{
            staticBean.setMin("").setMax("").setAvg("");
        }
        staticBean.setHistoryList(listcur);
        return staticBean;
    }

    @Override
    public StaticBean txPowerStatic(String device_id, long start_time, long end_time) {
        List<SmokeData> list = mongoEventDao.getTxPowerHistory(device_id,start_time,end_time);
        double max=0;
        double min=0;
        double sum=0;
        long count = 0;
        List<List<Object>> listcur = new ArrayList<>();
        for (SmokeData map:list) {
            long value = map.getTx_Power();

                count++;
                if(count==1){
                    max = value;
                    min = value;
                }

                sum+=value;
                if(max<value){
                    max = value;
                }
                if(min>value){
                    min = value;
                }
                long timestamp = map.getTimestamp();
                List<Object> lista = new ArrayList<>();
                lista.add(timestamp);
                lista.add(value);
                listcur.add(lista);
        }

        StaticBean staticBean = new StaticBean();
        if(count>0){
            staticBean.setMin(DataUtils.getDoubleString(min)).setMax(DataUtils.getDoubleString(max)).setAvg(String.format("%.2f", sum/count));
        }else{
            staticBean.setMin("").setMax("").setAvg("");
        }
        staticBean.setHistoryList(listcur);

        return staticBean;
    }

    @Override
    public StaticBean sinrStatic(String device_id, long start_time, long end_time) {
        List<HeartData> list = mongoHeartDao.getHeartBeatList(device_id,start_time,end_time);
        List<List<Object>> liss = new ArrayList<>();
        for (int i=0;i<list.size();i++){
            HeartData heartData = list.get(i);
            List<Object> l = new ArrayList<>();
            l.add(heartData.getTimestamp());
            l.add(heartData.getSinr());
            liss.add(l);
        }

        StaticBean staticBean =mongoHeartDao.getSinrStatic(device_id,start_time,end_time);
        if(staticBean==null){
            staticBean=new StaticBean().setAvg("").setMax("").setMin("").setHistoryList(liss);
        }else{
            staticBean.setHistoryList(liss);
        }

        return staticBean;
    }

    @Override
    public StaticBean batteryLevelStatic(String device_id, long start_time, long end_time) {

        List<SmokeData> list = mongoEventDao.getBatteryValueList(device_id,start_time,end_time);
        double max=0;
        double min=0;
        double sum=0;
        long count = 0;
        List<List<Object>> listbattery = new ArrayList<>();
        for (SmokeData map:list) {
            long value = map.getBattery_value();
            count++;
            if(count==1){
                max = value;
                min = value;
            }

            sum+=value;
            if(max<value){
                max = value;
            }
            if(min>value){
                min = value;
            }
            long timestamp = map.getTimestamp();
            List<Object> lista = new ArrayList<>();
            lista.add(timestamp);
            lista.add(value);
            listbattery.add(lista);

        }

        StaticBean staticBean = new StaticBean();
        if(count>0){
            staticBean.setMin(DataUtils.getDoubleString(min)).setMax(DataUtils.getDoubleString(max)).setAvg(String.format("%.2f", sum/count));
        }else{
            staticBean.setMin("").setMax("").setAvg("");
        }
        staticBean.setHistoryList(listbattery);

        return staticBean;
    }

    @Override
    public StaticBean batteryVoltageStatic(String device_id, long start_time, long end_time) {

        List<HeartData> list = mongoHeartDao.getHeartBeatList(device_id,start_time,end_time);
        List<List<Object>> listcur = new ArrayList<>();
        for (int i=0;i<list.size();i++){
            HeartData heartData = list.get(i);
            List<Object> lista = new ArrayList<>();
            lista.add(heartData.getTimestamp());
            lista.add(heartData.getBattery_voltage());
            listcur.add(lista);
        }

        StaticBean staticBean = mongoHeartDao.getBatteryVoltageStatic(device_id,start_time,end_time);

        if(staticBean==null){
            staticBean=new StaticBean().setAvg("").setMax("").setMin("").setHistoryList(listcur);
        }else{
            staticBean.setHistoryList(listcur);
        }

        return staticBean;
    }

    @Override
    public StaticBean smokeLevelStatic(String device_id, long start_time, long end_time) {
        List<SmokeData> list = mongoEventDao.getSmokeValueList(device_id,start_time,end_time);
        double max=0;
        double min=0;
        double sum=0;
        long count = 0;
        List<List<Object>> listcur = new ArrayList<>();
        for (SmokeData map:list) {
            long ad = map.getSmoke_value();
            count++;
            if(count==1){
                max = ad;
                min = ad;
            }

            sum+=ad;
            if(max<ad){
                max = ad;
            }
            if(min>ad){
                min = ad;
            }
            long timestamp = map.getTimestamp();
            List<Object> lista = new ArrayList<>();
            lista.add(timestamp);
            lista.add(ad);
            listcur.add(lista);
        }
        StaticBean staticBean = new StaticBean();
        if(count>0){
            staticBean.setMin(DataUtils.getDoubleString(min)).setMax(DataUtils.getDoubleString(max)).setAvg(String.format("%.2f", sum/count));
        }else{
            staticBean.setMin("").setMax("").setAvg("");
        }
        staticBean.setHistoryList(listcur);

        return staticBean;
    }
}
